package com.jiefeng.manager.service.springsecurity;

import com.jiefeng.manager.table.springsecurity.Role;
import com.jiefeng.manager.table.springsecurity.User;
import com.jiefeng.manager.table.springsecurity.UserRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class MyUserDetailsService implements UserDetailsService , ApplicationContextAware, InitializingBean{

    private static final Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);

    private ApplicationContext applicationContext;

    private UserRoleServiceDao userRoleServiceDao;

    protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();

    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        System.out.println("*****************************************************************************");
        User user = userRoleServiceDao.queryUserByUsername(userName);
        if(null == user) {
            throw new UsernameNotFoundException("User " + userName + " has not found************");   
        }
        if(user.getUserRoles().size()==0)
        {
            logger.debug("User '" + userName + "' has no authorities and will be treated as 'not found'");
            throw new UsernameNotFoundException(String.format("userDetailsService.noAuthority User {0} has no GrantedAuthority", userName));
        }
        List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>();
        for(UserRole userRole : user.getUserRoles())
        {
            Role role=userRole.getRole();
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getAuthorityName());
            dbAuths.add(authority);
        }
        MyUserDetails myUserDetails=new MyUserDetails(user.getUsername(),user.getPassword(), user.isDisabled(), true, true, true,  dbAuths);
        System.out.println("*******用户的信息：MyUserDetails为："+myUserDetails.getPassword()+"********");
        return  myUserDetails;   

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.userRoleServiceDao = applicationContext.getBean(UserRoleServiceDao.class);
    }
}
